<?php session_start();
	include('../controller/functions.php');
	require_once '../controller/validation.php';
	User::connection();
			ini_set("SMTP","127.0.0.1");
			ini_set("smtp_port","25");
			ini_set('sendmail_from', 'marketeers.group@gmail.com');

$userArr	= array('admin','teacher','student');
	if(!in_array($_SESSION['user'],$userArr)) {
	User::validSession();

}
if ($_GET['q'] == 'logout') 
{
	User::user_logout();
	print '		
			<script type="text/javascript">		
			window.location = "../login_r.php";
			</script>
			';


}


?>


<script type="text/javascript" src="../javascripts/jquery.js"></script>
	<script type="text/javascript" src="../javascripts/validation.js"></script>
    <script type="text/javascript" src="../javascripts/jquery-ui-1.7.2.custom.min.js"> </script>
	
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
	"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<link rel="shortcut icon" href="../images/fav.ico">
<link rel="icon" type="image/gif" href="../images/fav.ico">
<title>Registration of Students</title>

	<link rel="stylesheet" href="../css/style2.css" type="text/css" charset="utf-8" />
 	<link rel="stylesheet" href="../css/general.css" type="text/css" charset="utf-8" />
    <link rel="stylesheet" href="../css/jquery-ui-1.7.2.custom.css" type="text/css" charset="utf-8" />
</head>
<body>

 <div id="page">
<div id="wrapper">
        
        		   <div class="header_top" id="header_top"> </div>
  </div>
  <div class="header2"></div> 
	<div class="header"><!--open logo -->
		  </div><!--end header -->
	<!-- close logo-->
<!-- hide the client inquiries php-->
    <?php
    echo '<div id="nav">';
    echo '<div class="underlinemenu">';
	include('menu_view.php');
	echo '</div>';
    echo '</div>';
	?>
    <div id="header_in"> <!--open header -->
       <p> </p>
    <table width="100%" height="56">
     <tr>
       <td width="317" align="left" class="date"> 
        <? User::oras(); ?>
        </td>
            
       <td width="388" align="right"><? User::showPic(); ?></td>
       <td width="279">&nbsp;&nbsp;<? User::showUser(); ?>        </td>
       </tr>
   </table>
    </div>
    <div id="ex">
    </div>
      <p> </p>
       <div id="body"> <!--open body -->
    
	 <div id="sidebar"> 
           <!-- <div id="chef_ind">open chef -->
        <div id="welcome"> <!--open welcome -->
               <!-- <h2 class="guilded"></h2> -->
        </div> <!--close welcome-->

<? echo '<p class="more">';
echo '';
 
   require('sideBar.php');
    
echo'</p>';
?>
  </div>
        
      <!--</div>close left -->
      <div id="content"> 
      <div class="desriptionHeader"> <!--open right-->
       <div class="post"><!--open booking -->
         <div class="post3"> <h1>Registration for Students</h1></div>
            <div class="desriptionHeader"> <!--open right-->
       <div class="post3"><!--open booking -->

       
		<?if( isset($_POST['send']) && (!validateFname($_POST['fname']) ||  !validateDOB($_POST['bday']) || !validateSex($_POST['sex']) || !validateMname($_POST['mname'])  || !validateEmail($_POST['email']) || !validateLname($_POST['lname']) ) ):?>
				<div id="error">
					<ul>
						<?if(!validateFname($_POST['fname'])):?>
							<li><strong>Invalid Firstname:</strong> We want Firstname with more than 3 letters!</li>
						<?endif?>
                        <?if(!validateMname($_POST['mname'])):?>
							<li><strong>Invalid Middlename:</strong> We want Firstname with more than 3 letters!</li>
						<?endif?>
                        
                        <?if(!validateLname($_POST['lname'])):?>
							<li><strong>Invalid Lastname:</strong> We want Firstname with more than 3 letters!</li>
						<?endif?>
                       	<?if(!validateEmail($_POST['email'])):?>
							<li><strong>Invalid E-mail:</strong> Hey Stop! Please type a valid e-mail please :P</li>
						<?endif?>
                        <?if(!validateSex($_POST['sex'])):?>
							<li><strong>Invalid Gender:</strong>Please Select Gender!</li>
						<?endif?>
                      <?if(!validateDOB($_POST['bday'])):?>
			<li><strong>Invalid Birthdate:</strong>Type a valid Date Of Birth, dd/mm/yyyy!</li>
					<?endif?>  
					
						
                     
					</ul>
				</div>
		<!--Registration for students -->
				
				<? elseif(isset($_POST['send'])):
				
				$String = "qwertyuiop@#$%^&*()asdfghjklzxcvbnm";
	$Length = 8; // Change this value to indicate how long your passwords should be. 32 character limit.
	$String = md5($String);
	$StringLength = strlen($String);
	$Begin =rand(0,($StringLength-$Length-1));; // Pick an arbitrary starting point.
	$password = substr($String, $Begin, $Length);
	
				
            		$query = sprintf("INSERT INTO tblUser(fname,mname,lname,birthdate,gender,email,password,user_type,datejoin,status,username,avatar,lastlogin) VALUES('$_POST[fname]','$_POST[mname]','$_POST[lname]','$_POST[bday]','$_POST[sex]','$_POST[email]','$password','student',NOW(),'no', CONCAT(SUBSTR('$_POST[fname]', 1, 1), '$_POST[lname]'),'defaultdp.png','none');", mysql_real_escape_string(strip_tags($_POST['email'])), mysql_real_escape_string(strip_tags($_POST['sex'])));
	$res = @mysql_query($query);
				   
			
			
				$message = "Student, ".$_POST['fname']." ".$_POST['lname']."
  
  			Welcome to Panghulo National Highschool!
  			You have successfully registered to our PHNS E-Learning System. You may login to your account using the folowing  
			details below.
   
  					 Username:".$_POST['email']." or Username will be given upon logging-in 
  					 Password:".$password."
					 
			Note: You may also change your password on the ACCOUNT SETTINGS section.
  
  			You can now login by clicking this link: http:\\elearning-pnhs.com
  
 			Thank your for being part of Panghulo National High School.
  
  			Admin ";
				
			$sub = "Registered at PHNS E-Learning";
			$headers = "X-Mailer: PHP/" . phpversion();
			
						
			$sentmail = mail($email, $sub, $message, $headers);
	if($sentmail){

          //print success message.
		  

          echo "<b>Thank you!You have been succesfully registered a Student!</b><br>You'll be redirected to List of Students after (4) Seconds";

          echo "<meta http-equiv=Refresh content=4;url=viewStudents.php>";
}
	
	if($res){
			print '
		
		<script type="text/javascript">
		
		alert("Please complete the fields.");
		window.location = "viewStudents.php";
		</script>
		
		
		';
	}
	else{
		echo '<div id="error">Error occured while saving!</div>';
	}  
	
	if(!$res)
		die("Error: ".mysql_error());
	else
		return $res;
			
            ?>
            
            <div id="error" class="valid">
					<ul>
						<li><strong>Congratulations!</strong> All fields are OK ;)</li>
					</ul>
				</div>	
		<?endif?>
		<form method="post" id="customForm" action="">
			<div>
				<label for="fname">Firstname</label>
				<input id="fname" name="fname" type="text" />
				<span id="fnameInfo">What's your Firstname?</span>
			</div>
            <div>
            
				<label for="mname">Middlename</label>
				<input id="mname" name="mname" type="text" />
				<span id="mnameInfo">What's your Middlename?</span>
			</div>
            <div>
            
				<label for="lname">Lastname</label>
				<input id="lname" name="lname" type="text" />
				<span id="lnameInfo">What's your Lastname?</span>
			</div>
            <div>
				 <label for="bday">Birthday</label>
				<input name="bday" type="text" size="10" id="datepicker" />
				<span id="bdayInfo">What's your is your birthday?</span>
            </div>
            
             <div>
      <label for="sex">Gender</label>
			<input type="radio" name="sex" value="male" />
       	Male <p> </p>
       	<input type="radio" name="sex" value="female"  />
       	Female	
		<span id="sexInfo">She or He?</span>
			</div>
			<div>
				<label for="email">E-mail</label>
				<input id="email" name="email" type="text" />
				<span id="emailInfo">Valid E-mail please, you will need it to log in!</span>
			</div>
			<div>
				<input id="send" name="send" type="submit" value="Send" />
			
		</form>
	      </div>

   	  </div><!--close booking -->
   </div><!--close desc -->
 
     				
	  </div><!--close booking -->
   </div><!--close desc -->
    	</div> <!--close right-->
      <div class="clear">&nbsp;</div>
 <!-- footer-->
 <?php 
	  echo '<div id="footer">';
     include('footer_view.php');
      echo '</div>';	
	  ?>

    </div><!-- close body-->
</div><!-- close page-->


</body>
</html>



